package cn.com.ccxe.core.db.util;

import java.io.Serializable;
import java.util.Comparator;

import cn.com.ccxe.core.entity.ArticleLink;
import cn.com.ccxe.core.util.CodeUtil;

import com.sleepycat.bind.tuple.TupleInput;


public class ArticleLinkCompartor implements Comparator<byte[]>,Serializable{

	/**
	 * 
	 */
	private static final long serialVersionUID = -2974594315539100396L;

	/**
	 * TODO 比较标准与测试文档的编写。
	 */
	public int compare(byte[] arg0, byte[] arg1) {
		ArticleLink a1 = new ArticleLinkTupleBinding().entryToObject(new TupleInput(arg0));
		ArticleLink a2 = new ArticleLinkTupleBinding().entryToObject(new TupleInput(arg1));
		if(a1.getPath().equals(a2.getPath()) 
				&& a1.getModuleId() == a2.getModuleId()) {
				return 0;
			}
			if(a1.getModuleId() != a2.getModuleId()) {
				return a1.getModuleId() - a2.getModuleId();
			}
			return code(a1.getPath()) - code(a2.getPath());
	}
	
	private int code(String str) {
		return CodeUtil.byteArray2Int(str.getBytes());
	}

}
